package com.ssy.lingxi.settle.accounts.serviceimpl;

import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.contract.api.feign.ContractControllerFeign;
import com.ssy.lingxi.contract.api.model.vo.request.ContractPageRequest;
import com.ssy.lingxi.contract.api.model.vo.response.ContractPageResponse;
import com.ssy.lingxi.marketing.api.feign.CouponControllerFeign;
import com.ssy.lingxi.marketing.api.model.request.CouponQueryReq;
import com.ssy.lingxi.marketing.api.model.response.CouponInfoResp;
import com.ssy.lingxi.message.api.feign.SystemMessageControllerFeign;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import com.ssy.lingxi.order.api.feign.OrderFeignService;
import com.ssy.lingxi.order.api.model.vo.request.*;
import com.ssy.lingxi.order.api.model.vo.response.*;
import com.ssy.lingxi.pay.api.dto.request.eAccount.EAccountOrderPayRequest;
import com.ssy.lingxi.pay.api.dto.request.eAccount.PlatFormRePayRequest;
import com.ssy.lingxi.pay.api.dto.request.eAccount.PlatFormTransferRequest;
import com.ssy.lingxi.pay.api.feign.IAllInPayFeign;
import com.ssy.lingxi.pay.api.model.vo.response.allInPay.ApplicationTransferRespVO;
import com.ssy.lingxi.pay.api.model.vo.response.allInPay.SignalAgentPayRespVO;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.enums.SettleAccountReceivableOperateTypeEnum;
import com.ssy.lingxi.report.api.feign.ReportControllerFeign;
import com.ssy.lingxi.report.api.vo.request.OperateNoticeVO;
import com.ssy.lingxi.settle.accounts.model.constant.ReceiptInvoiceStatusEnum;
import com.ssy.lingxi.settle.accounts.service.IFeignService;
import com.ssy.lingxi.settle.accounts.utils.FeignLogUtils;
import com.ssy.lingxi.workflow.api.feign.ProcessFeign;
import com.ssy.lingxi.workflow.api.model.contant.ProcessEnum;
import com.ssy.lingxi.workflow.api.model.vo.request.ExternalProcessQueryVO;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskExecuteVO;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskStartVO;
import com.ssy.lingxi.workflow.api.model.vo.response.ComplexTaskCompleteVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleProcessDefVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 内部接口实现
 * @author 伍将
 * @date 2020/11/26
 * @version 2.0.0
 */
@Service
public class FeignServiceImpl implements IFeignService {

    private static final Logger logger = LoggerFactory.getLogger(FeignServiceImpl.class);

    @Resource
    private ReportControllerFeign reportController;

    @Resource
    private SystemMessageControllerFeign systemMessageControllerFeign;
    @Resource
    private OrderFeignService orderFeignService;
    @Resource
    private ContractControllerFeign contractControllerFeign;
    @Resource
    private IAllInPayFeign iAllInPayFeign;
    @Resource
    private ProcessFeign processFeign;
    @Resource
    private CouponControllerFeign controllerFeign;

    /**
     * 发送系统消息
     * @author 伍将
     * @date 2020/12/22
     * @param request :
     **/
    @Async
    @Override
    public void sendSystemMessage(@RequestBody SystemMessageRequest request) {
        try {
            systemMessageControllerFeign.sendSystemMessage(request);
        } catch (Exception e) {
            logger.info("调用报表服务新增系统通知接口失败：{}", e.getMessage());
        }

    }

    /**
     * 批量发送系统消息
     * @author 伍将
     * @date 2021/1/20
     * @param request: 请求参数
     **/
    @Async
    @Override
    public void batchSendSystemMessage(List<SystemMessageRequest> request) {
        try {
            systemMessageControllerFeign.batchSendSystemMessage(request);
        } catch (Exception e) {
            logger.info("调用报表服务新增系统通知接口失败：{}", e.getMessage());
        }
    }

    /**
     * 新增操作通知
     * @author 伍将
     * @date 2020/11/26
     * @param operateNoticeVO : 操作通知
     **/
    private void addOperateNoticeNotice(OperateNoticeVO operateNoticeVO) {
        try {
            reportController.addOperateNotice(operateNoticeVO);
        } catch (Exception e) {
            logger.info("调用报表服务新增操作通知接口失败：{}", e.getMessage());
        }

    }

    /**
     * 批量新增系统操作通知
     * @author 伍将
     * @date 2021/1/20
     * @param operateVOList:
     **/
    @Async
    @Override
    public void addSystemOperateNoticeList(List<OperateNoticeVO> operateVOList) {
        try {
            reportController.addOperateNoticeList(operateVOList);
        } catch (Exception e) {
            logger.info("调用报表服务新增操作通知接口失败：{}", e.getMessage());
        }
    }

    /**
     * 新增开票操作通知
     * @param memberId :会员id
     * @param roleId :会员角色id
     * @param status :状态
     */
    @Override
    public void addReceiptInvoiceOperateNotice(Long memberId, Long roleId, Integer status) {
        Long count = 1L; // 变更数量默认为1
        OperateNoticeVO operateNoticeVO = new OperateNoticeVO();
        operateNoticeVO.setMemberId(memberId);
        operateNoticeVO.setRoleId(roleId);
        operateNoticeVO.setDataSource(OperateDataSourceEnum.SETTLE_ACCOUNT_RECEIVABLE.getCode());
        ReceiptInvoiceStatusEnum statusEnum = ReceiptInvoiceStatusEnum.getEnum(status);
        switch (statusEnum) {
            case NO:
                operateNoticeVO.setIncreaseOperateType(SettleAccountReceivableOperateTypeEnum.TO_BE_INVOICE.getCode());
                operateNoticeVO.setIncreaseCount(count);
                break;
            case YES:
                operateNoticeVO.setReduceOperateType(SettleAccountReceivableOperateTypeEnum.TO_BE_INVOICE.getCode());
                operateNoticeVO.setReduceCount(count);
                break;
        }

        addOperateNoticeNotice(operateNoticeVO);
    }

    @Override
    public Wrapper<OrderSettleAccountInvoiceFeignDetailVO> findSettleAccountInvoiceDetail(String orderNo) {
        OrderNoFeignVO feignVO=new OrderNoFeignVO();
        feignVO.setOrderNo(orderNo);
        Wrapper<OrderSettleAccountInvoiceFeignDetailVO> feignResult = orderFeignService.findSettleAccountInvoiceDetail(feignVO);
        FeignLogUtils.printLog("查询订单发票",feignResult,orderNo);
        return feignResult;
    }

    @Override
    public Wrapper<SignalAgentPayRespVO> allInPayEscrowPayment(PlatFormRePayRequest request) {
        Wrapper<SignalAgentPayRespVO> wrapper = iAllInPayFeign.platFormRePay(request);
        FeignLogUtils.printLog("通联支付-托管代付", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<String> allInPayConsumeApply(EAccountOrderPayRequest request) {
        Wrapper<String> wrapper = iAllInPayFeign.orderPay(request);
        FeignLogUtils.printLog("通联支付-消费申请 (商户直接到账)", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<List<PlatformSettlementTypeVO>> findSettlementType() {
        Wrapper<List<PlatformSettlementTypeVO>> wrapper = orderFeignService.findSettlementType();
        FeignLogUtils.printLog("获取结算支付方式", wrapper);
        return wrapper;
    }

    @Override
    public Wrapper<List<MemberSettleChannelVO>> findSettlementChannel(MemberSettleChannelQueryVO request) {
        Wrapper<List<MemberSettleChannelVO>> wrapper = orderFeignService.findSettlementChannel(request);
        FeignLogUtils.printLog("获取会员角色-通联支付-支付渠道", wrapper, request);
        return wrapper;
    }

//    @Override
//    public Wrapper<List<OrderAfterSalePaymentFeignDetailVO>> findOrderSettlementPayment(OrderIdsFeignVO request) {
//        Wrapper<List<OrderAfterSalePaymentFeignDetailVO>> wrapper = orderFeignService.findAfterSaleOrderPayments(request);
//        FeignLogUtils.printLog("获取订单-支付记录", wrapper, request);
//        return wrapper;
//    }

    @Override
    public Wrapper<ComplexTaskCompleteVO> startBusinessReconciliation(Long vendorMemberId, Long vendorRoleId, Long id) {
        TaskStartVO request = new TaskStartVO();
        request.setProcessKey(ProcessEnum.SETTLE_ACCOUNTS_BUSINESS_RECONCILIATION.getProcessKey());
        request.setMemberId(vendorMemberId);
        request.setRoleId(vendorRoleId);
        request.setDataId(id);
        Wrapper<ComplexTaskCompleteVO> wrapper = processFeign.startProcess(request);
        FeignLogUtils.printLog("业务对账-启动流程", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<ComplexTaskCompleteVO> completeBusinessReconciliation(TaskExecuteVO request) {
        Wrapper<ComplexTaskCompleteVO> wrapper = processFeign.completeTask(request);
        FeignLogUtils.printLog("业务对账-提交流程", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<SimpleProcessDefVO> getLogOtherState(ExternalProcessQueryVO request) {
        Wrapper<SimpleProcessDefVO> wrapper = processFeign.findSimpleExternalTaskDefinitions(request);
        FeignLogUtils.printLog("业务对账-查询外部流程", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<PageData<OrderBuyerSettleQueryVO>> findApplyAmountRowSourceOrders(OrderBuyerSettlePageVO queryParameter) {
        Wrapper<PageData<OrderBuyerSettleQueryVO>> wrapper = orderFeignService.pageSettleBuyerOrders(queryParameter);
        FeignLogUtils.printLog("新增请款单-查询待请款单据", wrapper, queryParameter);
        return wrapper;
    }

    @Override
    public Wrapper<PageData<ContractPageResponse>> findApplyAmountRowSourceContracts(ContractPageRequest queryParameter) {
        Wrapper<PageData<ContractPageResponse>> wrapper  = contractControllerFeign.pageContractByPartB(queryParameter);
        FeignLogUtils.printLog("新增请款单-查询待请款单据", wrapper, queryParameter);
        return wrapper;
    }

    @Override
    public Wrapper<ApplicationTransferRespVO> platFormTransfer(PlatFormTransferRequest request) {
        Wrapper<ApplicationTransferRespVO> wrapper = iAllInPayFeign.platFormTransfer(request);
        FeignLogUtils.printLog("平台结算管理-积分结算-通联付款", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<Void> updateOrderDeliveries(OrderUpdateDeliveriesFeignVO request) {
        Wrapper<Void> wrapper = orderFeignService.updateOrderDeliveries(request);
        FeignLogUtils.printLog("应付账款结算-付款-更新对应订单支付批次已结算", wrapper, request);
        return wrapper;
    }

    @Override
    public Wrapper<List<OrderBuyerSettleReconciliationFeignVO>> settleReconciliationBuyerOrders(OrderFeignIdsVO orderFeignIdsVO) {
        Wrapper<List<OrderBuyerSettleReconciliationFeignVO>> wrapper = orderFeignService.settleReconciliationBuyerOrders(orderFeignIdsVO);
        FeignLogUtils.printLog("结算服务 - 协同对账 - 对账单生成请款单采购订单数据", wrapper, orderFeignIdsVO);
        return wrapper;
    }

    @Override
    public Wrapper<List<CouponInfoResp>> getCouponDetailListByIds(CouponQueryReq request) {
        Wrapper<List<CouponInfoResp>> wrapper = controllerFeign.getCouponDetailListByIds(request);
        FeignLogUtils.printLog("结算服务 - 平台积分结算 - 根据优惠券详情ids，查询券码", wrapper, request);
        return wrapper;
    }
}
